/*
 * Copyright © 2009 Perseus Project - Tufts University <http://www.perseus.tufts.edu>
 *
 * This file is part of UniCollatorPerseus.
 *
 * OcroPerseus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * OcroPerseus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with OcroPerseus.  If not, see <http://www.gnu.org/licenses/>.
 */

package eu.himeros.ocr.image;

import eu.himeros.ocr.image.BoxMixer;
import eu.himeros.ocr.image.BoxPainter;
import eu.himeros.util.IoManager;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 *
 * @author Federico Boschetti <federico.boschetti.73@gmail.com>
 */
public class ImgMaker {
    RenderedImage rendImage=null;
    File file=null;
    BufferedImage buffImage=null;
    Graphics2D g2d=null;
    FontMetrics fm=null;
    Font font=null;
    BufferedReader br=null;
    BufferedWriter bw=null;
    String fontName=null;
    int fontStyle=0;
    int fontSize=0;
    boolean colx=false; //alternate colors
    int inter=0; //interspace between characters
    int width=3600;
    int height=3600;

    /**
     *
     */
    public ImgMaker(){}

    /**
     *
     * @param txtFile
     * @param imgFile
     * @param boxFile
     */
    public void writeImage(String txtFile,String imgFile,String boxFile){
        try{
            br=IoManager.getReader(txtFile);
            bw=IoManager.getWriter(boxFile);
            rendImage=makeImage();
            file=new File(imgFile);
            ImageIO.write(rendImage,"png", file);
        }catch(IOException e){e.printStackTrace();}
    }

    /**
     *
     * @param fontName
     * @param fontStyle
     * @param fontSize
     * @param colx
     */
    public void setParams(String fontName, String fontStyle, String fontSize, boolean colx){
        this.fontName=fontName;
        this.fontStyle=Integer.valueOf(fontStyle).intValue();
        this.fontSize=Integer.valueOf(fontSize).intValue();
        this.colx=colx;
    }

    /**
     *
     * @return
     */
    private RenderedImage makeImage(){  
        buffImage=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        g2d=buffImage.createGraphics();
        font=new Font(fontName,fontStyle,fontSize);
        fm=g2d.getFontMetrics(font);
        g2d.setFont(font);
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0,0, width, height);
        g2d.setColor(Color.BLACK);
        String line=null;
        int ht=fm.getHeight()+20;
        int i=0;
        char[] cs=null;
        int span=0;
        try{
            boolean red=true;
            while((line=br.readLine())!=null){
                i++;
                cs=line.toCharArray();
                span=20;
                for(int j=0;j<cs.length;j++){
                    if(cs[j]!=' '){
                        bw.write(""+cs[j]+" ");
                        bw.write(""+span+" ");                 
                        bw.write(""+(height-i*ht+fm.getDescent())+" ");
                        bw.write(""+(span+fm.charWidth(cs[j])-2+inter)+" ");
                        bw.write(""+(height-i*ht+fm.getHeight())+" ");
                        bw.newLine();
                    }else{
                        red=(red?false:true);
                    }
                    if(colx){
                        if(red){
                            g2d.setColor(new Color(255,0,0));
                            red=false;
                        }else{
                            g2d.setColor(new Color(0,0,255));
                            red=true;
                        }
                    }else{
                        g2d.setColor(Color.BLACK);
                    }
                    g2d.drawChars(cs,j,1,span+inter,i*ht);
                    span+=fm.charWidth(cs[j])+inter;
                }
            }
            bw.close();
        }catch(Exception e){e.printStackTrace();}
        g2d.dispose();
        return buffImage;
    }

    /**
     *
     * @param args
     */
    public void makeBox(String[] args){
        BoxPainter bp=new BoxPainter(args[0], "true");
        BoxMixer bm=new BoxMixer(args);
        bp=new BoxPainter(args[0], "false");
    }

    /**
     *
     * @param fileName
     */
    public void end(String fileName){
            Runtime runtime=Runtime.getRuntime();
            try{
                Process proc=runtime.exec("rm "+fileName+"_.box");proc.waitFor();
                proc=runtime.exec("rm "+fileName+"_.txt");proc.waitFor();
                proc=runtime.exec("rm "+fileName+"_.png");proc.waitFor();
                proc=runtime.exec("rm "+fileName+".pbm");proc.waitFor();
            }catch(Exception ex){ex.printStackTrace();}
    }

    /**
     *
     * @param inter
     */
    public void setInter(int inter){
        this.inter=inter;
    }

    /**
     *
     * @param width
     */
    public void setWidth(int width){
        this.width=width;
    }

    /**
     *
     * @param height
     */
    public void setHeight(int height){
        this.height=height;
    }

    /**
     *
     * @param args
     */
    public static void main(String[] args){
        ImgMaker im=new ImgMaker();
        im.setInter(Integer.parseInt(args[5]));
        im.setWidth(Integer.parseInt(args[6]));
        im.setHeight(Integer.parseInt(args[7]));
        im.setParams(args[1],args[2],args[3],Boolean.parseBoolean(args[4]));
        im.writeImage(args[0]+".txt",args[0]+".png",args[0]+".box");
        im.makeBox(args);
        im.end(args[0]); 
    }

}
